package com.zhongge.service.impl;

import com.zhongge.entity.Clazz;
import com.zhongge.mapper.ClassMapper;
import com.zhongge.service.IClassService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhongge
 * @since 2025-11-09
 */
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Clazz> implements IClassService {
    //注入对应的持久层
    @Autowired
    private ClassMapper classMapper;

    /**
     * 返回如下树形结构
     * [
     *   {
     *     "id": "classManagement",
     *     "text": "班级管理",
     *     "children": [
     *       {
     *         "id": "grade_2023",
     *         "text": "2023级",
     *         "attributes": {
     *           "type": "grade"
     *         },
     *         "children": [
     *           {
     *             "id": "class_1",
     *             "text": "23软件班",
     *             "attributes": {
     *               "type": "class"
     *             }
     *           },
     *           {
     *             "id": "class_3",
     *             "text": "23智能班",
     *             "attributes": {
     *               "type": "class"
     *             }
     *           }
     *         ]
     *       },
     *       {
     *         "id": "grade_2022",
     *         "text": "2022级",
     *         "attributes": {
     *           "type": "grade"
     *         },
     *         "children": [
     *           {
     *             "id": "class_2",
     *             "text": "22软件班",
     *             "attributes": {
     *               "type": "class"
     *             }
     *           },
     *           {
     *             "id": "class_4",
     *             "text": "22智能班",
     *             "attributes": {
     *               "type": "class"
     *             }
     *           }
     *         ]
     *       }
     *     ]
     *   },
     *   {
     *     "id": "holidayManagement",
     *     "text": "节假日管理",
     *     "children": [
     *       {
     *         "id": "holidayPublish",
     *         "text": "节假日发布",
     *         "attributes": {
     *           "url": "holiday-publish.html"
     *         }
     *       },
     *       {
     *         "id": "statisticsReport",
     *         "text": "报表",
     *         "attributes": {
     *           "url": "statistics-report.html"
     *         }
     *       },
     *       {
     *         "id": "dataExport",
     *         "text": "数据导出",
     *         "attributes": {
     *           "url": "data-export.html"
     *         }
     *       }
     *     ]
     *   },
     *   {
     *     "id": "personalInfo",
     *     "text": "个人信息管理",
     *     "children": [
     *       {
     *         "id": "editInfo",
     *         "text": "修改信息",
     *         "attributes": {
     *           "url": "personal-info.html"
     *         }
     *       }
     *     ]
     *   }
     * ]
     */
    @Override
    public List<Map<String, Object>> getClazzInfoByTeacherID(Integer id) {
        //创建出总的集合：根节点集合：
        List<Map<String, Object>> nodeList = new ArrayList<>();

        /**************************教师相关菜单*******************/
        //查询出所有的年级：
        List<Clazz> allClazzInfo = classMapper.getClazzInfoByTeacherID(id);

        //我将这个所有年级信息变为流 再将其进行分组:变为流stream() 分组的人collect  使用分组器Collectors.groupingBy
        //根据什么分组：Clazz -> Clazz.getGrade())
        Map<String, List<Clazz>> collect = allClazzInfo.stream().collect(Collectors.groupingBy(Clazz -> Clazz.getGrade()));

        //构建出年级节点
        List<Map<String, Object>> gradeNode = new ArrayList<>();

        //我将上述的Map<String, List<Clazz>> 变为 什么呢？==》变为一个集合
        for (Map.Entry<String, List<Clazz>> entry: collect.entrySet()) {
            //分别取出key 和 value
            String key = entry.getKey();
            List<Clazz> value = entry.getValue();

            //将年级信息存于我们的map这种
            Map<String, Object> mapGrade = new HashMap<>();
            mapGrade.put("id","grade_"+key);
            mapGrade.put("text",key+"级");
            mapGrade.put("attributes",Collections.singletonMap("type","grade"));

            //创建出班级节点
            //由于有多个班级 就有多个map 这多个map就存于集合中
            List<Map<String,Object>> clazzNode = new ArrayList<>();
            for (Clazz clazzInfo: value) {
                //每一个班级就是一个节点 就是一个map
                Map<String, Object> mapClazz = new HashMap<>();
                mapClazz.put("id",clazzInfo.getId());
                mapClazz.put("text",clazzInfo.getName());
                mapClazz.put("attributes",Collections.singletonMap("type", "class"));

                clazzNode.add(mapClazz);
            }


            //继续将年级节点完成
            mapGrade.put("children",clazzNode);

            //将这些个年级存入集合中
            gradeNode.add(mapGrade);
        }
        //最后处理根节点了
        // 创建根节点 Map
        Map<String, Object> root = new HashMap<>();
        root.put("id", "classManagement");
        root.put("text", "班级管理");
        root.put("children", gradeNode);



        /***************节假日管理相关菜单************/
        //先处理子节点
        List<Map<String,Object>> holidayChildNodes = new ArrayList<>();

        //第一个元素（每一个元素是一个Map集合）
        Map<String,Object> holidayPublish = new HashMap<>();
        holidayPublish.put("id","holidayPublish");
        holidayPublish.put("text","节假日发布");
        holidayPublish.put("attributes", Collections.singletonMap("url", "/holiday/holidayPublish"));

        //第二个元素
        Map<String,Object> statisticsReport = new HashMap<>();
        statisticsReport.put("id","statisticsReport");
        statisticsReport.put("text","报表");
        statisticsReport.put("attributes",Collections.singletonMap("url", "/holiday/statisticsReport"));

        //第三个元素
        Map<String,Object> dataExport= new HashMap<>();
        dataExport.put("id","dataExport");
        dataExport.put("text","数据导出");
        dataExport.put("attributes",Collections.singletonMap("url", "/holiday/dataExport"));

        //将他们加入集合中
        holidayChildNodes.add(holidayPublish);
        holidayChildNodes.add(statisticsReport);
        holidayChildNodes.add(dataExport);

        //最后处理跟节点
        Map<String,Object> holidayRoot = new HashMap<>();
        holidayRoot.put("id","holidayManagement");
        holidayRoot.put("text","节假日管理");
        holidayRoot.put("children",holidayChildNodes);

        /***************个人信息管理相关菜单************/
        //先处理子节点
        List<Map<String,Object>> personalInfoNodes = new ArrayList<>();
        //第一个元素（每一个元素是一个Map集合）
        Map<String,Object> editInfo = new HashMap<>();
        editInfo.put("id","editInfo");
        editInfo.put("text","修改信息");
        editInfo.put("attributes",Collections.singletonMap("url", "/teacher/personalInfo"));

        personalInfoNodes.add(editInfo);

        Map<String,Object> personalInfoRoot = new HashMap<>();
        personalInfoRoot.put("id","personalInfo");
        personalInfoRoot.put("text","个人信息管理");
        personalInfoRoot.put("children",personalInfoNodes);



        /***************最后将所有跟元素加入List集合并返回************/
        nodeList.add(root);//班级管理菜单相关
        nodeList.add(holidayRoot);//节假日发布菜单相关
        nodeList.add(personalInfoRoot);//个人信息管理菜单相关

        //最后将这个集合返回
        return nodeList;
    }


    // 后端需要提供树形结构的班级数据
    // 数据格式示例：
    // [
    //   {
    //     "id": "classManagement",
    //     "text": "班级管理",
    //     "children": [
    //       {
    //         "id": "grade_2023",
    //         "text": "2023级",
    //         "attributes": {"type": "grade"},
    //         "children": [
    //           {"id": "class_2301", "text": "23软件班", "attributes": {"type": "class"}}
    //         ]
    //       }
    //     ]
    //   }
    // ]
    /*
    @Override
    public List<Map<String, Object>> getClazzInfoByTeacherID(Integer id) {

        //查询出所有的年级：
        List<Clazz> allClazzInfo = classMapper.getClazzInfoByTeacherID(id);

        //我将这个所有年级信息变为流 再将其进行分组:变为流stream() 分组的人collect  使用分组器Collectors.groupingBy
        //根据什么分组：Clazz -> Clazz.getGrade())
        Map<String, List<Clazz>> collect = allClazzInfo.stream().collect(Collectors.groupingBy(Clazz -> Clazz.getGrade()));
        
        //构建出年级节点
        List<Map<String, Object>> gradeNode = new ArrayList<>();
        
        //我将上述的Map<String, List<Clazz>> 变为 什么呢？==》变为一个集合
        for (Map.Entry<String, List<Clazz>> entry: collect.entrySet()) {
            //分别取出key 和 value
            String key = entry.getKey();
            List<Clazz> value = entry.getValue();

            //将年级信息存于我们的map这种
            Map<String, Object> mapGrade = new HashMap<>();
            mapGrade.put("id","grade_"+key);
            mapGrade.put("text",key+"级");
            mapGrade.put("attributes",Collections.singletonMap("type","grade"));

            //创建出班级节点
            //由于有多个班级 就有多个map 这多个map就存于集合中
            List<Map<String,Object>> clazzNode = new ArrayList<>();
            for (Clazz clazzInfo: value) {
                //每一个班级就是一个节点 就是一个map
                Map<String, Object> mapClazz = new HashMap<>();
                mapClazz.put("id","class_"+clazzInfo.getId());
                mapClazz.put("text",clazzInfo.getName());
                mapClazz.put("attributes",Collections.singletonMap("type","class"));

                clazzNode.add(mapClazz);
            }

            //继续将年级节点完成
            mapGrade.put("children",clazzNode);

            //将这些个年级存入集合中
            gradeNode.add(mapGrade);
        }
        //最后处理根节点了
        // 创建根节点 Map
        Map<String, Object> root = new HashMap<>();
        root.put("id", "classManagement");
        root.put("text", "班级管理");
        root.put("children", gradeNode);

        //最后将根节点变为set集合返回
        return Arrays.asList(root);
    }*/
    //这里已经实现了基本的增删改查功能，所以基本不用写
}
